<?php
/**
 * 服务管理
 * 
 * @author ShuangYa
 * @package APIGateway
 * @category Controller
 * @link https://www.sylingd.com/
 * @copyright Copyright (c) 2018 ShuangYa
 * @license https://www.sylibs.com/go/apigateway/license
 */
namespace ag\controller\admin;

use yesf\library\ControllerAbstract;

use ag\library\Utils;
use ag\model\Service as ServiceModel;
use ag\library\Dispatcher;

class Service extends ControllerAbstract {
	/**
	 * 获取服务列表
	 * 
	 * @api {get} /admin/service/list 获取服务列表
	 * @apiName GetServiceList
	 * @apiGroup Admin
	 * 
	 * @apiSuccess {Object[]} list 服务列表
	 * @apiSuccess {Int} list.id 服务ID
	 * @apiSuccess {String} list.name 服务名称
	 */
	public static function listAction($request, $response) {
		$result = [];
		$list = Dispatcher::getRegistered();
		foreach ($list as $k => $v) {
			$result[] = [
				'id' => $k,
				'name' => $v[0]
			];
		}
		$response->write(Utils::getWebApiResult([
			'list' => $result
		]));
	}
	/**
	 * 获取服务下的所有在线服务器
	 * 
	 * @api {get} /admin/service/server 获取服务下的所有在线服务器
	 * @apiName GetServerList
	 * @apiGroup Admin
	 * 
	 * @apiParam {Int} id 服务ID
	 * 
	 * @apiSuccess {Object[]} list 服务器列表
	 * @apiSuccess {Int} list.service 服务ID
	 * @apiSuccess {String} list.ip IP
	 * @apiSuccess {String} list.port 端口
	 * @apiSuccess {String} list.weight 权重
	 */
	public static function serverAction($request, $response) {
		$id = intval($request->get['id']);
		$list = ServiceModel::getInstance()->list([
			'service' => $id
		]);
		$response->write(Utils::getWebApiResult([
			'list' => $list
		]));
	}
}